package teamproject.visualization.gui.visualobjects;

import javax.media.j3d.Appearance;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Material;
import javax.media.j3d.RenderingAttributes;
import javax.media.j3d.TransparencyAttributes;
import javax.vecmath.Color3f;

import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Primitive;

public class TransparentBuffer implements VisualObject {

	private Appearance appearance;
	private int id;
	private Cylinder shape;
	private RenderingAttributes ra;

	public TransparentBuffer(int id, float radius, float height) {
		this.ra = new RenderingAttributes();
		this.ra.setCapability(RenderingAttributes.ALLOW_VISIBLE_WRITE);
		this.setID(id);
		this.appearance = createAppearance();
		this.shape = new Cylinder(radius, height, Primitive.GENERATE_NORMALS,
				appearance);
	}

	private Appearance createAppearance() {
		Appearance app = new Appearance();
		app.setRenderingAttributes(ra);
		TransparencyAttributes ta = new TransparencyAttributes();
		ta.setTransparencyMode(TransparencyAttributes.BLENDED);
		ta.setTransparency(0.4f);
		app.setTransparencyAttributes(ta);

		// color
		ColoringAttributes caColor = new ColoringAttributes();
		caColor.setColor(1.0f, 0.5f, 0.3f);

		caColor.setShadeModel(ColoringAttributes.SHADE_GOURAUD);
		app.setColoringAttributes(caColor);

		// Light properties
		Color3f objColor = new Color3f(0.27f, 0.91f, 0.91f);
		Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
		app.setMaterial(new Material(objColor, black, objColor, black, 80.0f));

		return app;
	}

	public int getID() {
		return id;
	}

	public Cylinder getShape() {
		return shape;
	}

	@Override
	public boolean isVisible() {
		return ra.getVisible();
	}

	public void setColor(Color3f color) {
		shape.getAppearance().getMaterial().setAmbientColor(color);
		shape.getAppearance().getMaterial()
				.setEmissiveColor(new Color3f(0.0f, 0.0f, 0.0f));
		shape.getAppearance().getMaterial().setDiffuseColor(color);
		shape.getAppearance().getMaterial()
				.setSpecularColor(new Color3f(0.0f, 0.0f, 0.0f));
		shape.getAppearance().getMaterial().setShininess(80.0f);
	}

	public void setID(int id) {
		this.id = id;
	}

	@Override
	public void setVisible(boolean visible) {
		ra.setVisible(visible);
	}
}
